﻿using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Auth.OAuth2.Requests;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CloudStorageLight.Core
{
    [ExcludeFromCodeCoverage]
    internal class DictionaryDataStore : IDataStore
    {
        public Dictionary<string, object> Data { get; set; }
        public string RefreshToken { get; set; }

        public DictionaryDataStore()
            : this(null)
        {
        }

        public DictionaryDataStore(string refreshToken)
        {
            this.RefreshToken = refreshToken;
            Data = new Dictionary<string, object>();
        }

        public Task StoreAsync<T>(string key, T value)
        {
            Data[key] = value;
            return TaskEx.Delay(0);
        }

        public Task DeleteAsync<T>(string key)
        {
            Data.Remove(key);
            return TaskEx.Delay(0);
        }


        public Task<T> GetAsync<T>(string key)
        {
            TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
            try
            {
                var data = Data.ContainsKey(key) ? (T)Data[key] : default(T);
                tcs.SetResult(data);
            }
            catch (Exception ex)
            {
                tcs.SetException(ex);
            }

            return tcs.Task;
        }

        public Task ClearAsync()
        {
            Data.Clear();
            return TaskEx.Delay(0);
        }
    }

}
